#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'app/init.php';
require_once APPPATH.'../tests/Test.php';
require_once APPPATH.'../tests/AirHttpTest.php';
require_once APPPATH.'../tests/models/TestSource.php';
require_once APPPATH.'../tests/AirTestUtils.php';
require_once APPPATH.'../tests/models/TestUser.php';
require_once APPPATH.'../tests/models/TestOrganization.php';

define('MY_TEST_PASS', 'fooBar123.');

//plan('no plan');
plan(12);

AIR2_DBManager::init();

// Create 4 users in 3 different orgs.
$users = array();
$count = 0;
while ($count++ < 4) {
    $u = new TestUser();
    $u->user_password = MY_TEST_PASS;
    $u->save();
    $users[] = $u;
}

// test Orgs
$orgs = array();
$count = 0;
while ($count++ < 3) {
    $o = new TestOrganization();
    $o->save();
    $orgs[] = $o;
}

$orgs[0]->add_users(array($users[0]));
$orgs[0]->save();
$orgs[1]->add_users(array($users[2]), 4);  // MANAGER
$orgs[1]->save();
$orgs[2]->add_users(array($users[3]));
$orgs[2]->add_users(array($users[1]), 3);  // WRITER
$orgs[2]->save();

$source = new TestSource();
$source->save();
$source->add_orgs(array($orgs[01], $orgs[2]));
$source->save();
//actual tests
is($source->user_may_read($users[1]), AIR2_AUTHZ_IS_ORG,
    "users[1] may read Source if associated by Organization");
is($source->user_may_read($users[0]), AIR2_AUTHZ_IS_DENIED,
    "users[0] may not read if not associated by Organizatio");
is($source->user_may_write($users[1]), AIR2_AUTHZ_IS_ORG,
    "users[1] may write if in Organization and WRITER");
is($source->user_may_write($users[3]), AIR2_AUTHZ_IS_DENIED,
    "users[3] may not write if not in Organization");
is($source->user_may_manage($users[2]), AIR2_AUTHZ_IS_ORG,
    "user[2] in organization may manage.");
is($source->user_may_manage($users[3]), AIR2_AUTHZ_IS_DENIED,
    "users[3] may not manange if they cannot even write");

$q = AIR2_Query::create();
$q->from('Source s');
Source::query_may_read($q, $users[0]);
is( $q->count(), 0, "users[0] may not view if not associated by org.");

$q = AIR2_Query::create();
$q->from('Source s');
Source::query_may_read($q, $users[3]);
is( $q->count(), 1, "users[3] may view if associated by org.");

$q = AIR2_Query::create();
$q->from('Source s');
Source::query_may_write($q, $users[3]);
is( $q->count(), 0, "users[3] may not write if not associated by org.");

$q = AIR2_Query::create();
$q->from('Source s');
Source::query_may_write($q, $users[1]);
is( $q->count(), 1, "users[1] may write if associated by org and writer.");

$q = AIR2_Query::create();
$q->from('Source s');
Source::query_may_manage($q, $users[1]);
is( $q->count(), 0, "users[1] may not manage if associated by org but not manager");

$q = AIR2_Query::create();
$q->from('Source s');
Source::query_may_manage($q, $users[2]);
is( $q->count(), 1, "users[2] may write if associated by org and writer.");